import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import { type App } from 'vue'

/** 获取router目录下的每个路由模块暴露的路由表 */
const routeList: RouteRecordRaw[] = []

const modules: Record<string, any> = import.meta.glob('./modules/*ts', {
  eager: true
})

// console.log('modules', modules)
// console.log('Object.keys(modules)', Object.keys(modules))

Object.keys(modules).forEach((key) => {
  // console.log(modules[key])
  routeList.push(modules[key].default)
})

console.log('routeList', routeList)

const routes: RouteRecordRaw[] = []

const router = createRouter({
  routes: routes.concat(routeList), // 路由表
  history: createWebHistory(), // 路由的模式，这里是history模式
  // 路由跳转滚动行为
  async scrollBehavior() {
    return await new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          left: 0,
          top: 0,
          behavior: 'smooth'
        })
      }, 200)
    })
  }
})

/** 安装vue-router */
export function setupRouter(app: App): void {
  app.use(router)
}

/** 向外暴露路由实例对象 */
export default router
